﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PSSMS.View.SysManage
{
    /// <summary>
    /// CleAdd.xaml 的交互逻辑
    /// </summary>
    public partial class CleAdd : Window
    {
        BLL.Clientsm.ClientsmClient myClient = new BLL.Clientsm.ClientsmClient();

        public CleAdd()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗口事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //下拉框绑定
            DataTable dt = myClient.SelectSrType().Tables[0];
            khlx.ItemsSource = dt.DefaultView;
            khlx.DisplayMemberPath = "customerType";
            khlx.SelectedValuePath = "customerTypeID";

            DataTable ds = myClient.SelDisName().Tables[0];
            ssdq.ItemsSource = ds.DefaultView;
            ssdq.DisplayMemberPath = "disName";
            ssdq.SelectedValuePath = "disID";

            DataTable dd = myClient.SelectPmethod().Tables[0];
            ssywy.ItemsSource = dd.DefaultView;//显示数据
            ssywy.DisplayMemberPath = "employeeName";
            ssywy.SelectedValuePath = "employeeID";
        }
        /// <summary>
        /// 确定、保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、必填项不能为空
                if (!string.IsNullOrEmpty(khmc.Text.Trim()))
                {
                    //2、获取控件值
                    string strCustomerName = khmc.Text.Trim();//客户名称
                    string strContactPerson = lxr.Text.Trim();//联系人
                    string strContactNumber = lxdh.Text.Trim();//联系电话 
                    int intCustomerTypeID = Convert.ToInt32(khlx.SelectedValue);//客户类型
                    int intDisID = Convert.ToInt32(ssdq.SelectedValue);//所属地区
                    string strContactAddress = lxdz.Text.Trim();//联系地址
                    bool blDefaultNo = (bool)mrkh.IsChecked;//默认客户
                    bool blDisabledNo = (bool)jy.IsChecked;//启用、禁用
                    string strRemark = bz.Text.Trim();//备注
                    int intEmployeeID = Convert.ToInt32(ssywy.SelectedValue);//业务员

                    //3、新增信息：获取返回值,与方法一样的参数
                    int intCount = myClient.InsertManage(intCustomerTypeID, strCustomerName,
         strContactPerson,strContactNumber,intDisID,strContactAddress,
         intEmployeeID,strRemark,blDefaultNo,blDisabledNo);
                    if (intCount > 0)
                    {
                        MessageBoxResult dr = MessageBox.Show("新增成功！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information); //弹出确定对话框
                        this.Close();
                    }
                    else if (intCount == -1)
                    {
                        MessageBox.Show("新增信息重复！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                    }
                    else
                    {
                        MessageBox.Show("新增失败！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                    }
                }
                else
                {
                    MessageBox.Show("请输入客户名称，并检查是否输入完整！", "系统提示：", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("关闭窗口，无法保存数据，是否要关闭？", "系统提示：", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                //关闭窗口
                this.Close();
            }
        }
    }
}
